
//const util = require("../../../common/js/util");
var app = getApp();
import Qs from "qs";
import util from "/common/js/util";

Page({
  data: {
    parameter: "",
    gotoUrl: "",
    show:false
  },
  onLoad(e) {
    
    // http://test.yuehong.pro/#/dd_file_view
    // e = {
    //   ADD_TIME: "2022-04-26 15:02:16"
    //   ENCLOSURE_ID: "4b55cd7a51fc41788b680447c7458071"
    //   FILE_NAME: "OOOJKKK"
    //   FILE_TYPE: ".doc"
    //   PATH: "uploadFiles/uploadImgs/20220426/6d8c1c47ef934c60b161ac7f294c0aa1.doc"
    //   STATUS: 1
    //   TYPE_NAME: "PurchaseContractPic4"
    //   icon: "/static/img/office-word.png"
    //   is_web_view: 1
    //   name: "OOOJKKK"
    //   url: "http://test.yuehong.pro/YhERP/uploadFiles/uploadImgs/20220426/6d8c1c47ef934c60b161ac7f294c0aa1.doc"
    // }

    /** json内部属性
     * url
     * httpUrl null  1  是一个网站跳转链接
     */

    let json = e;
  

    let parameter = "?" + Qs.stringify(e);
    let time = new Date().getTime();
    let fileUrl = `${util.isNotEmpty(json.url)?json.url:json.PATH}?time=${time}`;
 //   fileUrl=util.urlHttpToHttps(fileUrl)

    // let gotoUrl=`https://view.officeapps.live.com/op/view.aspx?src=${e.url}`
    // let gotoUrl=`http://test.yuehong.pro/#/dd_file_view${parameter}`
    let gotoUrl = util.isNotEmpty(json.url)?json.url:json.PATH;

  //  console.log("---gotoUrl::",gotoUrl);

    // console.log(parameter);
    if (app.prototype.platform == "iOS") {
      //ios
      if (json.FILE_TYPE.indexOf("pdf") != -1) {
        gotoUrl=fileUrl;
      }else{
        let is_file=this.isFileFn(fileUrl);
        if(is_file){
          gotoUrl = `https://view.officeapps.live.com/op/view.aspx?src=${fileUrl}`;
        }else{
          gotoUrl=fileUrl;
        }

      }
    } else {
      //安卓
      // excel
      if (json.FILE_TYPE.indexOf("xls") != -1) {
        gotoUrl = `https://view.officeapps.live.com/op/view.aspx?src=${fileUrl}`
      } else if (json.FILE_TYPE.indexOf("doc") != -1) {
        gotoUrl = `http://view.officeapps.live.com/op/view.aspx?src=${fileUrl}`;
  //      gotoUrl=fileUrl;

      } else {
        gotoUrl=fileUrl;
        
      }
    }

    this.setData({
      parameter, gotoUrl
    })
    this.setData({
      show:true
    })
    //dd.setStorageSync({ key: "p", data: e });
    this.webViewContext = dd.createWebViewContext('web-view-1');
    this.webViewContext.postMessage(json);
  },
  isFileFn(url){
    let is_file=false;
    let f_arr=[".pdf",".xls",".doc",".docx"];
    f_arr.forEach(r=>{
      if (r.indexOf(".pdf") != -1) {
        is_file=true
      }
    });
   return is_file;
  },
  onmessage(e) {
    console.log("---小程序收到消息:---");
    console.log(e);
  },
});
